<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
            <el-form-item label="卡号" prop="cardNo">
                <el-input v-model="queryParams.cardNo" placeholder="请输入卡号" clearable @keyup.enter.native="handleQuery"
                    maxlength="30" show-word-limit style="width:250px;" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table :data="tCardList" ref="tCard">
            <el-table-column label="序号" align="center" type="index" width="55" />
            <el-table-column label="卡号" align="center" prop="cardNo" min-width="200" :show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column label="卡密" align="center" prop="cardPwd" min-width="150" :show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column label="卡券类型编号" align="center" prop="cardTypeId" min-width="150"
                :show-overflow-tooltip="true" :formatter="typeFormatter">
            </el-table-column>
            <el-table-column label="计量方式" align="center" prop="cardMode" min-width="150" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <div v-if="scope.row.cardMode == 1">观影次数卡</div>
                    <div v-if="scope.row.cardMode == 2">积分卡</div>
                </template>
            </el-table-column>
            <el-table-column label="原始金额或次数" align="center" prop="faceValue" min-width="150"
                :show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column label="剩余金额或次数" align="center" prop="balance" min-width="150"
                :show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column label="卡券状态" align="center" prop="status" min-width="150" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <div v-if="scope.row.status == 0">初始</div>
                    <div v-if="scope.row.status == 1">已激活</div>
                    <div v-if="scope.row.status == 2">已用完</div>
                    <div v-if="scope.row.status == 3">已过期</div>
                    <div v-if="scope.row.status == 4">锁定</div>
                </template>
            </el-table-column>
            <el-table-column label="是否激活" align="center" prop="isBound" min-width="150" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <div v-if="scope.row.isBound == 0">未绑定</div>
                    <div v-if="scope.row.isBound == 1">已绑定</div>
                </template>
            </el-table-column>
            <el-table-column label="是否已完全消费" align="center" prop="isConsumed" min-width="150"
                :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <div v-if="scope.row.isBound == 0">未完全消费</div>
                    <div v-if="scope.row.isBound == 1">已完全消费</div>
                    <!-- <el-input v-model="scope.row.isConsumed" placeholder="请输入是否已完全消费" /> -->
                </template>
            </el-table-column>
            <el-table-column label="激活人ID" align="center" prop="userId" min-width="150" :show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column label="用户账号" align="center" prop="userName" min-width="150" :show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column label="手机号码" align="center" prop="phonenumber" min-width="150"
                :show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column label="激活时间" align="center" prop="activatedTime" min-width="150"
                :show-overflow-tooltip="true">
                <!-- <template slot-scope="scope">
                        <el-date-picker clearable v-model="scope.row.activatedTime" type="date"
                            value-format="yyyy-MM-dd" placeholder="请选择激活时间" />
                    </template> -->
            </el-table-column>
            <el-table-column label="最后使用时间" align="center" prop="usedTime" min-width="150"
                :show-overflow-tooltip="true">
                <!-- <template slot-scope="scope">
                        <el-date-picker clearable v-model="scope.row.usedTime" type="date" value-format="yyyy-MM-dd"
                            placeholder="请选择最后使用时间" />
                    </template> -->
            </el-table-column>
            <el-table-column label="有效开始日期" align="center" prop="validStartDate" min-width="150"
                :show-overflow-tooltip="true">
                <!-- <template slot-scope="scope">
                        <el-date-picker clearable v-model="scope.row.validStartDate" type="date"
                            value-format="yyyy-MM-dd" placeholder="请选择有效开始日期" />
                    </template> -->
            </el-table-column>
            <el-table-column label="有效结束日期" align="center" prop="validEndDate" min-width="150"
                :show-overflow-tooltip="true">
                <!-- <template slot-scope="scope">
                        <el-date-picker clearable v-model="scope.row.validEndDate" type="date" value-format="yyyy-MM-dd"
                            placeholder="请选择有效结束日期" />
                    </template> -->
            </el-table-column>
            <el-table-column label="是否能兑换电影" align="center" prop="movieEnabled" min-width="150"
                :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <div v-if="scope.row.movieEnabled == 0">能</div>
                    <div v-if="scope.row.movieEnabled == 1">不能</div>
                    <!-- <el-input v-model="scope.row.movieEnabled" placeholder="请输入是否能兑换电影启用，0=能，1=不能" /> -->
                </template>
            </el-table-column>
            <el-table-column label="是否能兑换福利" align="center" prop="welfareEnabled" min-width="150"
                :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <div v-if="scope.row.welfareEnabled == 0">能</div>
                    <div v-if="scope.row.welfareEnabled == 1">不能</div>
                    <!-- <el-input v-model="scope.row.welfareEnabled" placeholder="请输入是否能兑换福利，0=能，1=不能" /> -->
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="80">
                <template slot-scope="scope">
                   <el-button size="mini" type="text" @click="handleRecords(scope.row)"
                        v-hasPermi="['film:filmCard:records']">使用记录</el-button>
                    <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
                        v-hasPermi="['film:filmCard:edit']">修改</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="cardTotal > 0" :total="cardTotal" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改企业卡券申请订单对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="剩余金额或次数" prop="balance">
                    <el-input v-model="form.balance" placeholder="请输入剩余金额或次数" maxlength="10" show-word-limit />
                </el-form-item>
                <el-form-item label="有效结束日期" prop="validEndDate">
                    <el-date-picker v-model="form.validEndDate" type="date" format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd" placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listCards, cardIds ,updatefilmCard,cardUsageRecordList,cardorders,cardordersdetail} from "@/api/company/cardOrderDetail";
import { listselectType } from "@/api/cardType/cardType";
export default {
    name: "CardOrder",
    data() {
        return {
            cardTypeList: [],
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            cardTotal: 0,
            // 企业卡券申请订单表格数据
            tCardList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                cardNo: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                orderNo: [
                    { required: true, message: "订单编号不能为空", trigger: "blur" }
                ],
                totalAmount: [
                    { required: true, message: "订单总金额不能为空", trigger: "blur" }
                ]
            }
        };
    },
   async created() {
       await listselectType().then(res => {
            this.cardTypeList = res.rows;
        })
        this.getList();
    },
    methods: {
        handleRecords(){
              this.$router.push({
                path:'/orderCouponRecords/index',
                query:{
                    cardId:row.id
                }
            })
        },
        typeFormatter(row) {
            let data = this.cardTypeList.find(item => row.cardTypeId == item.id)
            return data.typeCode ||'未知类型'
        },
        /** 查询企业卡券申请订单列表 */
        getList() {
            this.loading = true;
            this.queryParams.cardId = this.$route.query.cardId
            cardUsageRecordList(this.queryParams).then(res => {
                this.tCardList = res.rows
                this.cardTotal = res.total
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                orderNo: null,
                totalAmount: null,
                remark: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加企业卡券申请订单";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            cardIds(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改卡券信息";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    updatefilmCard(this.form).then(response => {
                        this.$modal.msgSuccess("修改成功");
                        this.open = false;
                        this.getList();
                    });
                }
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('/film/card/export', { orderDetailId: this.$route.query.orderDetailId }, `卡券列表_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
